######################################################################################################################
# fig4: Comparison among the newly defined macropartisanship indices for the US, the UK, Japan, Germany, and Denmark #
######################################################################################################################


library(ggplot2)
library(tidyverse)
library(zoo)
library(gridExtra)
library(psych)
library(cowplot)
library(lubridate)





###-----usa


#load data
usa<-read.csv("USA_FINAL.csv")
attach(usa)

#calculating classic macroaprtisanship
usa$perdemocrat<-democrat/(democrat+republican)

#1st step:basic fomulation for the new index
usa$macropartisanship_1<-(democrat/(democrat+republican))*(enpp_np/2)*(((democrat+republican)/independent)*(1/enpp_np^2))
describe(usa$macropartisanship_1)

#2nd step: calculating variances
usa$macrop_var<-var(usa$macropartisanship_1)
usa$macroper_var<-var(usa$perdemocrat)

#3rd step: weighting process
usa$macropartisanship_USA<-(usa$macropartisanship_1/usa$macrop_var+usa$perdemocrat/usa$macroper_var)/(1/usa$macrop_var+1/usa$macroper_var)


#setting dataset
df<-data.frame(usa$yearMon,usa$perdemocrat,usa$macropartisanship_USA) 


write.csv(df,"usa_macropartisanship.csv")




###-------german
df_meangr<-read.csv("GERMAN_FINAL.csv")
attach(df_meangr)

#calculating the sum of percentage and the percentage of the independent
allp_gr=.n+ADM_ALL+AFD_AL+ALFA+andere.P+BP.Bay+BundFre+Bundnis+Burgerpa+BuSo.Bu+
  CDU+CM_Chri+DA+DasNeue+DBD+DieViol+DKP+DSU+DVU+Familie+Feminist+FWD_Fr+
  GAZ+GLU+Graue.Pa+Grune+MLPD_M+NPD+ODP+ODPD+PBC_Pa+PDS.die+Piratenp+PSG_Pa+
  Rentnerp+Republik+RRP...Re+Schill.P+SPD+Statt.Pa+Tierschu+WASG_w+Zentrum
indep_gr=1-allp_gr

#1st step:basic fomulation for the new index

df_meangr$macropartisanship1<-(CDU/allp_gr)*(enpp_np/2)*((allp_gr/indep_gr)*(1/enpp_np^2))
describe(df_meangr$macropartisanship1)
df_meangr$perCDU<-CDU/allp_gr

#2nd step: calculating variances
df_meangr$macrop_var<-var(df_meangr$macropartisanship1)
df_meangr$macroper_var<-var(df_meangr$perCDU)

#3rd step: weighting process
df_meangr$macropartisanship_GR<-(df_meangr$macropartisanship1/df_meangr$macrop_var+df_meangr$perCDU/df_meangr$macroper_var)/
  (1/df_meangr$macrop_var+1/df_meangr$macroper_var)


#setting dataset
df<-data.frame(df_meangr$yearMon,df_meangr$perCDU,df_meangr$macropartisanship_GR) 

write.csv(df,"german_macropartisanship.csv")



###-------denmark
denmark_final<-read.csv("DENMARK_FINAL.csv")
attach(denmark_final)
denmark_final$indep_dnk=1-(denmark_final$allp_dnk/100)
denmark_final$socd<-mean.socd/100

#1st step:basic fomulation for the new index

denmark_final$macropartisanship_1<-(denmark_final$socd/(denmark_final$allp_dnk/100))*(enpp_np/2)*(((denmark_final$allp_dnk/100)/denmark_final$indep_dnk)*(1/enpp_np^2))
describe(denmark_final$macropartisanship_1)
denmark_final$perSOCD<-denmark_final$socd/(denmark_final$allp_dnk/100)

#2nd step: calculating variances
denmark_final$macropartisanship_1<-ifelse((is.infinite(denmark_final$macropartisanship_1) & (denmark_final$macropartisanship_1> 0)) , 0.01, denmark_final$macropartisanship_1)
denmark_final$macrop_var<-var(denmark_final$macropartisanship_1,na.rm=T)
denmark_final$macroper_var<-var(denmark_final$socd,na.rm=T)

#3rd step: weighting process
denmark_final$macropartisanship_DNK<-(denmark_final$macropartisanship_1/denmark_final$macrop_var+denmark_final$perSOCD/denmark_final$macroper_var)/
  (1/denmark_final$macrop_var+1/denmark_final$macroper_var)


#setting dataset
df<-data.frame(denmark_final$yearMon,denmark_final$socd,denmark_final$macropartisanship_DNK) 

write.csv(df,"denmark_macropartisanship.csv")


###-------uk
df_mp_uk<-read.csv("UK_FINAL.csv")
attach(df_mp_uk)
summary(df_mp_uk)
df_mp_uk$percon<-df_mp_uk$dfmean_con/df_mp_uk$allp_uk

#1st step:basic fomulation for the new index

df_mp_uk$macropartisanship1<-(dfmean_con.mean.Con/df_mp_uk$allp_uk)*(enpp_np/2)*((allp_uk/perindependent)*(1/enpp_np^2))
describe(df_mp_uk$macropartisanship1)

#2nd step: calculating variances
df_mp_uk$macropartisanship1<-ifelse((is.infinite(df_mp_uk$macropartisanship1) & (df_mp_uk$macropartisanship1> 0)) , 0.01, df_mp_uk$macropartisanship1)
macrop_var<-var(df_mp_uk$macropartisanship1)
macroper_var<-var(df_mp_uk$percon)

#3rd step: weighting process
df_mp_uk$macropartisanship_UK<-(df_mp_uk$macropartisanship1/macrop_var+df_mp_uk$percon/macroper_var)/
  (1/macrop_var+1/macroper_var)


#setting dataset 
df<-data.frame(df_mp_uk$yearMon,df_mp_uk$macropartisanship_UK,df_mp_uk$percon) 

write.csv(df,"uk_macropartisanship.csv")


###--------japan
#load data
b<-read.csv("JAPAN_FINAL.csv")
attach(b)

#percentage of ldp support
b$ldp_share<-ldp/allp
indep_jp<-100-allp

###efficient party numbers: with vote share
#squared values of each party share
ob<-cbind(jldp_v,jjsp_v,jjcp_v,jother_v,jindep_v,jminkai_v,jminnna_v,jminsha_v,jminshu_v,
          jkomei_v,jkokuminshin_v,jkokoro_v,jnewlib_v,jsdf_v,jshinpo_v,jshinsei_v,jshinshin_v,jshintonihon_v,
          jsakigake_v,jjiyu_v,jjiyuren_v,jjisedai_v,jhoshu_v,jhoshushin_v,jkaikaku_v,jseikatu_j,
          jrikken_v,jkibou_v)


#1st step:basic fomulation for the new index
b$macropartisanship1<-(ldp/100)*(enpp_np/2)*((allp/indep_jp)*(1/enpp_np^2))
describe(b$macropartisanship1)


#2nd step: calculating variances
b$macrop_var<-var(b$macropartisanship1,na.rm=T)
b$macroper_var<-var(b$ldp_share,na.rm=T)

#3rd step: weighting process
b$macropartisanship_jp<-(b$macropartisanship1/b$macrop_var+b$ldp_share/b$macroper_var)/(1/b$macrop_var+1/b$macroper_var)

#setting dataset
df<-data.frame(b$yearMon,b$ldp_share,b$macropartisanship_jp) 


write.csv(df,"japan_macropartisanship.csv")


###---------creating Figure 4

#reload each data

usam<-read.csv("usa_macropartisanship.csv")
ukm<-read.csv("uk_macropartisanship.csv")
germanm<-read.csv("german_macropartisanship.csv")
denmarkm<-read.csv("denmark_macropartisanship.csv")
japanm<-read.csv("japan_macropartisanship.csv")

#change colnames
colnames(usam)<-c("","yearMon","mp1","USA")
usam<-usam[,-1]
colnames(ukm)<-c("","yearMon","mp1","UK")
ukm<-ukm[,-1]
colnames(germanm)<-c("","yearMon","mp1","German")
germanm<-germanm[,-1]
colnames(denmarkm)<-c("","yearMon","mp1","Denmark")
denmarkm<-denmarkm[,-1]
colnames(japanm)<-c("","yearMon","mp1","Japan")
japanm<-japanm[,-1]

#unified 5 datasets
df_1<-left_join(usam,ukm,by="yearMon")
df_2<-left_join(df_1,germanm,by="yearMon")
df_3<-left_join(df_2,denmarkm,by="yearMon")
df_unified<-left_join(df_3,japanm,by="yearMon")

#resetting dataframe
df_mp<-data.frame(df_unified$yearMon,df_unified$Denmark,
                  df_unified$German,df_unified$Japan,
                   df_unified$UK,df_unified$USA)

#resetting colnames

colnames(df_mp)<-c("yearMon","DEN","GER","JPN","UK","USA")
des_dfmp<-describe(df_mp,ranges=F,trim = F)
xtable(des_dfmp)
summary(df_mp)

#plot and save
df_mp_figure<-df_mp %>% 
  gather(key="variable", value="value", -yearMon)

fig4<-ggplot(df_mp_figure, aes(x = as.yearmon(yearMon), 
                               y = value)) + 
  geom_line(aes(color = variable), size = 1) +
  labs(color="")+
  theme_minimal()+
  xlab("Year")+
  ggtitle("")

windows(10,10)
fig4

#save image
ggsave(file = "fig4.pdf", plot = fig4)


